<!--
 * @Author: cgl
 * @Date: 2023-04-18 15:12:26
 * @LastEditors: cgl
 * @LastEditTime: 2023-08-24 10:22:36
 * @FilePath: \public-health-emergencies-fed:\radiation-hygiene-fe\src\views\slide\contranctTask.vue
 * @Description:
 *
 * Copyright (c) 2023 by cgl/微盛信息产业有限公司, All Rights Reserved.
-->
<template>
  <div class="app-container">
    <el-form
      class="searchform"
      :model="queryParams"
      ref="queryParams"
      size="small"
      label-width="80px"
    >
      <el-row :gutter="20">
        <el-col :span="5">
          <el-form-item label="单位名称" label-width="80px">
            <m-select
              v-model="queryParams.entrustOrganizationId"
              placeholder="输入文字检索查询"
              style="width: 100%"
              filterable
              clearable
              :lists="entrustOrganizationSelect"
              lists_label="organizationName"
              lists_value="entrustOrganizationId"
              lists_key="entrustOrganizationId"
              :total="orgTotal"
              :pageSize="10"
              @handleChange="listeningEvent"
            >
            </m-select>
          </el-form-item>
        </el-col>
        <el-col :span="9">
          <el-form-item label-width="20px">
            <el-button
              type="primary"
              icon="el-icon-search"
              size="mini"
              @click="getList()"
              >搜索</el-button
            >
            <el-button icon="el-icon-refresh" size="mini" @click="reset()"
              >重置</el-button
            >
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>

    <div class="table-wrapper">
      <div class="tableoperation">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click.native.prevent="openDialog()"
          >登记修改</el-button
        >
      </div>

      <el-tabs v-model="activeName" @tab-click="changeTab" class="tab">
        <el-tab-pane label="待审批" name="1"></el-tab-pane>
        <el-tab-pane label="已签发" name="2"></el-tab-pane>
        <el-tab-pane label="已拒绝" name="3"></el-tab-pane>
      </el-tabs>

      <el-table
        v-loading="loading"
        :data="contractList"
        @row-dblclick="(row) => openDialog(row, '3')"
      >
        <el-table-column
          label="单位名称"
          align="center"
          prop="entrustOrganizationName"
          :show-overflow-tooltip="true"
        />
        <el-table-column
          label="联系人"
          align="center"
          prop="contactsName"
          width="150"
        />
        <el-table-column
          label="联系电话"
          align="center"
          prop="contactsMobile"
          width="150"
        />
        <el-table-column
          label="单位地址"
          align="center"
          prop="organizationAddress"
          :show-overflow-tooltip="true"
        />
        <el-table-column
          label="批次"
          align="center"
          prop="batchNum"
          :show-overflow-tooltip="true"
        />
        <el-table-column
          label="佩戴时间"
          align="center"
          prop="organizationAddress"
          :show-overflow-tooltip="true"
        >
          <template slot-scope="{ row }">
            {{ row.wearingBeginTime + "-" + row.wearingEndTime }}
          </template>
        </el-table-column>
        <el-table-column align="center" label="操作">
          <template slot-scope="{ row }">
            <el-link
              style="margin-left: 4px"
              type="primary"
              :underline="false"
              @click.native.prevent="handleFlowRecord(row)"
              >流程</el-link
            >
            <el-link
              style="margin-left: 4px"
              type="primary"
              v-if="row.tasksState == 1 && row.canAudit == 1"
              :underline="false"
              @click.native.prevent="handleProcess(row, '1')"
              >办理</el-link
            >
            <el-link
              style="margin-left: 4px"
              type="danger"
              v-if="row.tasksState == 1"
              :underline="false"
              @click.native.prevent="handleProcess(row, '2')"
              >驳回</el-link
            >
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-show="total > 0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </div>

    <!-- 选择报告任务弹框 -->
    <el-dialog
      :title="title"
      :visible.sync="dialogVisible"
      :close-on-click-modal="false"
      width="1100px"
      class="adddialog"
      v-dialogDrag="true"
    >
      <el-form
        :model="form"
        :rules="rules"
        ref="form"
        size="small"
        label-width="100px"
      >
        <el-row>
          <el-col :span="8">
            <el-form-item label="单位名称" prop="entrustOrganizationId">
              <m-select
                v-model="form.entrustOrganizationId"
                :dimStr="form.organizationName"
                :placeholder="status != '3' ? '输入文字检索查询' : ''"
                style="width: 95%"
                filterable
                @change="changeEntrustOrganization"
                clearable
                :lists="entrustOrganizationSelect"
                lists_label="organizationName"
                lists_value="entrustOrganizationId"
                lists_key="entrustOrganizationId"
                :total="orgTotal"
                :pageSize="10"
                @handleChange="listeningEvent"
              >
              </m-select>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>

      <el-table :data="issuedLists">
        <el-table-column label="批次" align="center" prop="batchNum" />
        <el-table-column label="佩戴时间" align="center">
          <template slot-scope="{ row }">
            {{ row.wearingBeginTime + "-" + row.wearingEndTime }}
          </template>
        </el-table-column>
        <el-table-column align="center" label="操作">
          <template slot-scope="{ row }">
            <el-button @click.native.prevent="viewFile(row)" type="text">
              查看
            </el-button>
            <el-button
              @click.native.prevent="handleContractApply(row)"
              type="text"
            >
              发起
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-dialog>

    <!-- 任务修改发起 -->
    <el-dialog
      :title="initiateTitle"
      :visible.sync="initiateDialogVisible"
      :close-on-click-modal="false"
      width="1100px"
      class="adddialog"
      v-dialogDrag="true"
    >
      <el-table :data="initiateLists">
        <el-table-column label="姓名" align="center" prop="batchNum">
          <template slot-scope="{ row }">
            <el-input
              v-model="row.personnelName"
              placeholder="请输入姓名"
            ></el-input>
          </template>
        </el-table-column>
        <el-table-column label="性别" align="center">
          <template slot-scope="{ row }">
            <el-select
              size="mini"
              v-model="row.personnelSex"
              style="width: 100%"
            >
              <el-option
                v-for="item in dict.type.sys_user_sex"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              >
              </el-option>
            </el-select>
          </template>
        </el-table-column>
        <el-table-column label="职业类别" align="center">
          <template slot-scope="{ row }">
            <el-select
              size="mini"
              v-model="row.occupationalClassification"
              style="width: 100%"
            >
              <el-option
                v-for="item in dict.type.occupational_classification"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              >
              </el-option>
            </el-select>
          </template>
        </el-table-column>
        <el-table-column label="剂量计佩戴起止日期" align="center">
          <template slot-scope="{ row }">
            <el-date-picker
              size="mini"
              v-model="row.wearingTimes"
              class="wearingedit"
              style="width: 100%"
              value-format="yyyy-MM-dd HH:mm:ss"
              type="daterange"
              :clearable="false"
              @change="(val) => handleChangeWearingTime(row, val)"
            >
            </el-date-picker>
          </template>
        </el-table-column>
        <el-table-column label="孔一读出值" align="center">
          <template slot-scope="{ row }">
            <el-input
              v-model="row.holeOne"
              placeholder="请输入孔一值"
            ></el-input>
          </template>
        </el-table-column>
        <el-table-column label="孔二读出值" align="center">
          <template slot-scope="{ row }">
            <el-input
              v-model="row.holeTwo"
              placeholder="请输入孔二值"
            ></el-input>
          </template>
        </el-table-column>
      </el-table>
      <template #footer>
        <el-button type="primary" @click="submitForm">办 理</el-button>
      </template>
    </el-dialog>

    <!-- 意见弹框 -->
    <el-dialog
      title="审批意见"
      :visible.sync="manageVisible"
      :close-on-click-modal="false"
      width="600px"
      v-dialogDrag="true"
    >
      <el-input
        v-model="manageForm.comment"
        type="textarea"
        placeholder="请输入审批意见"
        clearable
      />
      <div slot="footer">
        <el-button type="primary" @click="submitManageData">确 定</el-button>
      </div>
    </el-dialog>
    <WorkFlow ref="workFlow"></WorkFlow>
  </div>
</template>

<script>
import {
  getReviseLists,
  getReviseInfo,
  getReviseHandleData,
  addReviseQuest,
  reviseSubmitCheck,
  reviseHandle,
  getListByIssued,
} from "@/api/slides/leaveMark";
// import { contractProcess, contractApply, contractList, getInspectionList } from "@/api/slides/leaveMark";
import {
  entrustOrganizationDetail,
  entrustOrganizationList,
} from "@/api/person/entrustOrganization";
import { listByIds } from "@/api/system/oss";
import { entrustEquipmentTypeListing } from "@/api/person/entrustEquipmentType";
import WorkFlow from "@/components/WorkFlow";
import MSelect from "@/components/mSelect";

export default {
  name: "LeaveMark",
  dicts: ["sys_user_sex", "occupational_classification"],
  components: { WorkFlow, MSelect },
  data() {
    return {
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        tasksState: "1",
      },
      // 视图高度
      height: document.documentElement.clientHeight,
      total: 0,
      dialogVisible: false,
      // 表单参数
      form: {},
      loading: false,
      status: null,
      title: null,
      rules: {
        entrustOrganizationId: [
          { required: true, message: "单位名称不能为空", trigger: "change" },
        ],
      },
      // 合同列表
      contractList: [],
      // 委托单位下拉框
      entrustOrganizationSelect: [],
      // 设备类型下拉框
      entrustEquipmentTypeSelect: [],
      // 检测类型下拉框
      inspectionList: [],
      CGInspectList: [],
      FHInspectList: [],
      radioInspectList: [],
      // 合计
      totalprice: 0,
      activeName: "1",
      isPer: false,
      isHide: false,
      // 单位查询数据
      organizationQueryData: {
        organizationName: "", //单位名称模糊查询
        pageNum: 1, //页码
        pageSize: 10, //每页展示的条数
      },
      orgTotal: 0,
      manageVisible: false,
      manageForm: {},

      // 已签发报告列表
      issuedQueryParams: {
        pageSize: 10,
        pageNum: 1,
        entrustOrganizationId: "",
      },
      issuedLists: [],

      // 任务修改发起
      initiateDialogVisible: false,
      initiateTitle: "",
      initiateLists: [],
      nowDosimeterId: "",
    };
  },
  created() {
    this.getList();
    this.getEntrustEquipmentTypeListing();
  },
  methods: {
    // 报告预览
    viewFile(row) {
      if (row.ossId) {
        listByIds(row.ossId).then((response) => {
          // isRecords && this.handleIsPrint();
          window.open(response?.data[0]?.url, "PDF", "");
        });
      } else {
        this.$modal.msgWarning("暂无文件");
      }
    },
    // 切换tab栏
    changeTab(tab, event) {
      this.queryParams.tasksState = tab.name;
      this.getList();
    },
    // 获取合同任务列表
    getList() {
      this.loading = true;
      getReviseLists(this.queryParams)
        .then((response) => {
          this.contractList = response.rows;
          this.total = response.total;
          this.loading = false;
        })
        .catch((response) => {
          this.loading = false;
        });
    },
    // 获取单位下拉框
    listeningEvent(e) {
      let { queryName, pageNum, pageSize, type } = e;
      if (type == "scroll" && pageNum == this.organizationQueryData.pageNum)
        return;
      this.organizationQueryData.organizationName = queryName;
      this.organizationQueryData.pageNum = pageNum;
      this.organizationQueryData.pageSize = pageSize;
      entrustOrganizationList(this.organizationQueryData).then((response) => {
        if (type == "scroll") {
          this.entrustOrganizationSelect.push(...response.rows);
        } else {
          this.entrustOrganizationSelect = response.rows;
        }
        this.orgTotal = response.total;
      });
    },
    // 佩戴时间修改
    handleChangeWearingTime(row, value) {
      this.$set(row, "wearingBeginTime", value[0]);
      this.$set(row, "wearingEndTime", value[1]);
    },
    // 获取设备类型下拉框
    getEntrustEquipmentTypeListing() {
      entrustEquipmentTypeListing().then((response) => {
        this.entrustEquipmentTypeSelect = response.data;
      });
    },
    // 打开对话框
    openDialog(row, status) {
      this.title = "选择报告任务";
      this.resetForm("form");
      this.form = {};
      this.dialogVisible = true;
    },
    // 提交按钮
    submitForm() {
      reviseSubmitCheck({
        taskPersonalDosimeterId: this.nowDosimeterId,
        operationList: this.initiateLists,
      }).then((res) => {
        this.$alert(res.msg || "报告数据未修改，是否提交", "提示", {
          dangerouslyUseHTMLString: true,
          confirmButtonText: "确认提交",
          cancelButtonText: "取消",
        }).then((res) => {
          addReviseQuest({
            taskPersonalDosimeterId: this.nowDosimeterId,
            operationList: this.initiateLists,
          }).then((res) => {
            this.dialogVisible = false;
            this.initiateDialogVisible = false;
            this.getList();
          });
        });
      });
    },
    submitManageData() {
      reviseHandle(this.manageForm).then((response) => {
        this.dialogVisible = false;
        this.manageVisible = false;
        this.manageForm = {};
        this.getList();
        this.$modal.msgSuccess("操作成功");
      });
    },
    // 通过/驳回
    handleProcess(row, tasksState) {
      this.manageForm = {
        id: row.id,
        operation: tasksState,
      };
      this.manageVisible = true;
    },
    // 发起
    handleContractApply(row) {
      this.initiateTitle = "编辑";
      getReviseHandleData({
        id: row.taskPersonalDosimeterId,
      }).then((res) => {
        this.initiateLists = res.rows.map((item) => {
          item.wearingTimes = [item.wearingBeginTime, item.wearingEndTime];
          return item;
        });
        this.initiateDialogVisible = true;
        this.nowDosimeterId = row.taskPersonalDosimeterId;
      });
    },
    // 查看流程
    handleFlowRecord(row) {
      this.$refs.workFlow.openDialog(row);
    },
    // 更改委托单位下拉
    changeEntrustOrganization(val) {
      this.issuedQueryParams.entrustOrganizationId = val;
      getListByIssued(this.issuedQueryParams).then((res) => {
        this.issuedLists = res.data;
      });
    },
    // 更改设备类型下拉
    changeEquipmentType(val, index) {
      let selectEquipment =
        this.entrustEquipmentTypeSelect.find(
          (item) => item.entrustEquipmentTypeId == val
        ) || {};
      this.$nextTick(() => {
        this.$set(
          this.form.tasksContractItemList[index],
          "entrustEquipmentTypeName",
          this.$refs[`equipmentType_${index}`].selectedLabel
        );
        this.$set(
          this.form.tasksContractItemList[index],
          "largeCategory",
          selectEquipment.largeCategory
        );
      });
      // 重置值
      this.form.tasksContractItemList[index].CG = [];
      this.form.tasksContractItemList[index].FH = [];
      this.form.tasksContractItemList[index].radioInspecId = null;
    },
    // 查询重置
    reset() {
      this.queryParams = {
        pageNum: 1,
        pageSize: 10,
        state: this.isPer ? "0" : "1",
      };
      this.resetForm("queryParams");
      this.getList();
    },
  },
};
</script>

<style lang="scss" scoped>
.app-container {
  display: flex;
  flex-direction: column;
}

.operation {
  margin-bottom: 15px;
  .register {
    padding-inline: 30px;
  }
}
.uploadbutton {
  padding-inline: 20px;
}
.myupload {
  margin-bottom: 20px;
}
.result {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-top: 30px;
  .calc {
    width: min-content;
    margin-left: 20px;
    .el-button {
      margin: 0;
      &:nth-of-type(1) {
        margin-bottom: 10px;
      }
    }
  }
}
.nametitle {
  text-align: center;
  margin-bottom: 10px;
  font-weight: bolder;
  &::before {
    content: "* ";
    color: red;
  }
}
.el-table .warning-row {
  background: rgb(228, 13, 13);
}
.searchcontainer {
  display: flex;
  justify-content: space-between;
  align-items: center;

  margin-top: -10px;
}
.viewform {
  justify-content: flex-end;
}
.innertitle {
  margin-bottom: 10px;
  position: relative;
  color: #252d57;
  font-weight: bold;
  padding-left: 15px;
  font-size: 18px;
  line-height: 20px;
  &::before {
    content: "";
    width: 5px;
    height: 100%;
    background-color: #2a72ee;
    border-radius: 5px;
    position: absolute;
    left: 0;
    bottom: 0;
  }
  .addline {
    float: right;
    padding-inline: 25px;
    transform: translateY(-7px);
  }
}
.inspect {
  display: flex;
  align-items: center;
  justify-content: center;
  .inspectcheckbox {
    margin-right: 20px;
    display: flex;
    align-items: center;
  }
  .inspectradio {
    .el-radio {
      margin-right: 13px;
    }
  }
}
.adddialog ::v-deep .el-dialog__body {
  height: 600px;
}
.tableoperation {
  margin-bottom: 0;
}
.adddialog ::v-deep .el-table__body .el-table__cell {
  padding-block: 4px;
}
</style>
